package com.sangeng.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.fastjson.JSON;
import com.sangeng.annotation.SystemLog;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.CategoryDto;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.ExcelCategoryVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.utils.WebUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CategoryController
 * Package: com.sangeng.controller
 * Description:
 *
 * @Author hjh
 * @Create 2024/8/24 22:50
 * @Version 1.0
 */
@RestController()
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //获取所有分类接口
    @SystemLog(businessName ="获取所有分类接口")
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
       return categoryService.listAllCategory();
    }

    //分类的分页查询
    @SystemLog(businessName = "分页查询分类列表")
    @GetMapping("/list")
    public ResponseResult list(Long pageNum,Long pageSize,String name ,String status){
       return categoryService.ConditionsList(pageNum, pageSize, name , status);
    }

    //导出分类

    //TODO 权限控制相关内容
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void downloadFailedUsingJson(HttpServletResponse response) throws IOException {

        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            //获取需要导出的数据
            List<Category> list = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(list, ExcelCategoryVo.class);
            //把数据写入Excel
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @SystemLog(businessName = "新增分类")
    @PostMapping()
    public ResponseResult addCategory(@RequestBody CategoryDto categoryDto){
       return categoryService.addCategory(categoryDto);
    }

    @SystemLog(businessName = "修改分类回显的相关接口")
    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable Long id){
      return   categoryService.getCategoryById(id);
    }

    @SystemLog(businessName = "修改分类接口")
    @PutMapping()
    public ResponseResult updateCategory(@RequestBody CategoryDto categoryDto){
        return categoryService.updateCategory(categoryDto);
    }

    @SystemLog(businessName = "删除某个分类")
    @DeleteMapping("/{id}")
    public ResponseResult deleteCategory(@PathVariable Long id ){
        return  categoryService.deleteCategory(id);
    }

    @SystemLog(businessName = "修改分类状态接口")
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody CategoryDto categoryDto){
        return categoryService.changeStatus(categoryDto);
    }

}
